<?php
//-------------------------------------------------------------------------------------------------------
    require_once('../config.inc.php');
    require_once($config['app_dir'] . '/includes/db.class.php');
	require_once($config['app_dir'] . '/classes/stockplot.data.php');
    require_once($config['app_dir'] . '/classes/class.calc2.php');
    require_once($config['app_dir'] . '/includes/template.class.php');
    require_once($config['app_dir'] . '/includes/template.class.php');
    require_once($config['app_dir'] . '/classes/staff.class.php');
    //---------------------------------------------------------------------------------------------------
    $Staff = new objStaff($config['app_id']);
    $Content = new objTemplate();

    $StockParams = new objStockParams();

//see _performance.tpl
	$show_days = $config['show']['max_days'];//21;
	$min_show_days = $config['show']['min_days'];//17;
	$period = $config['show']['period'];//60*60*24 * $show_days;

//	$now = getdate(mktime()-60*60*24*7);
	$now = getdate(mktime());
	$finish = mktime(0,0,0, $now['mon'], $now['mday'], $now['year']);
	$start = mktime(0,0,0,1,1,2006);


	$bdate = adodb_date2('U', $StockParams->start);
	$edate = adodb_date2('U', $StockParams->finish);

	if($bdate < $start)
	{
		$bdate = $start;
		$edate = $bdate + $period;
	}
	if($edate > $finish)
	{
		$edate = $finish;
		if($bdate > $finish - 24 * 60 * 60 * $min_show_days)
		{
			/*
			print('Error');
			return;
			*/
			$bdate = $finish - 24 * 60 * 60 * $min_show_days;
		}
	}
	$StockParams->start = date("Y-m-d",$bdate);
	$StockParams->finish = date("Y-m-d",$edate);
/*
	print($StockParams->start);
	print('<br>');
	print($StockParams->finish);
*/
	$a_bdate = getdate($bdate);

	$next_date = $a_bdate;
	if(1 <= $next_date['mday'] && $next_date['mday'] <= 15)
		$next_date['mday'] = 16;
	else
	{
		$next_date['mday'] = 1;
		if(12 == $next_date['mon'])
		{
			$next_date['mon'] = 1;
			$next_date['year']++;
		}
		else
			$next_date['mon']++;
	}

	$prev_date = $a_bdate;
	if(2 <= $prev_date['mday'] && $prev_date['mday'] <= 16)
		$prev_date['mday'] = 1;
	else
	{
		$prev_date['mday'] = 16;
		if(1 == $prev_date['mon'])
		{
			$prev_date['mon'] = 12;
			$prev_date['year']--;
		}
		else
			$prev_date['mon']--;// = $next_date['mon'] - 1;
	}

	$time = mktime(0,0,0,$prev_date['mon'], $prev_date['mday'], $prev_date['year']);
	if($start > $time)
	{
		$Content->Assign('prev', 0);
	}
	else
	{
		$Content->Assign('prev', 1);
		$Content->Assign('p_start', date('Y-m-d', $time));
		$Content->Assign('p_finish', date('Y-m-d', $time+$period));
	}

	$time = mktime(0,0,0,$next_date['mon'], $next_date['mday'], $next_date['year']);
//	print($finish);
//	print('|');
//	print($time + 60*60*24*$min_show_days);
	if($finish < $time + 60*60*24*$min_show_days)
	{
//	if($StockParams->start == '2007-06-16')
		$Content->Assign('next', 0);
	}
	else
	{
		$Content->Assign('next', 1);
		$Content->Assign('n_start', date('Y-m-d', $time));
		if($time+$period < $finish)
			$Content->Assign('n_finish', date('Y-m-d', $time+$period));
		else
			$Content->Assign('n_finish', date('Y-m-d', $finish));
	}

    $StockModel = new objStockModel($StockParams->trade, $StockParams->start, $StockParams->finish, false, $Staff->getSystem());
//	$StockModel->Build();

	$title = 'IntelliForex - ' . $StockModel->GetPair() . ' Forecasts History ' .
		'[' . date('M j', $StockParams->UnixtimeDate($StockParams->start)) .
		' - ' . date('M j', $StockParams->UnixtimeDate($StockParams->finish)) .
		', ' . date('Y', $StockParams->UnixtimeDate($StockParams->start)) . '] ';
	$Content->Assign('title', $title);
	$Content->Assign('pair', $StockModel->GetPair());
	$Content->Assign('trade', $StockParams->trade);
	$Content->Assign('start', $StockParams->start);
	$Content->Assign('finish', $StockParams->finish);
    $Content->Assign('system', $Staff->getSystem()/*$StockParams->system*/);// XXX system
    
    //$Pairs = new objPairs();
    $Pairs = new objPairsSystems();
    $Pairs->SystemID = $Staff->getSystem();
    $Content->Assign('pairsArr', $Pairs->getAssocArr());


    $Content->Display('_history.tpl');
?>
